<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品种类" align="center" prop="productVariety">
        <template slot-scope="scope">
          <div v-if="scope.row.productVariety===1">
            信用贷
          </div>
        </template>
      </el-table-column>
      <el-table-column label="期数" align="center" prop="numberLoan" />
      <el-table-column label="利率(年)" align="center" prop="rate">
        <template slot-scope="scope">
          {{scope.row.rate*100+"%"}}
        </template>
      </el-table-column>
      <el-table-column label="手续费" align="center" prop="premium">
        <template slot-scope="scope">
          {{scope.row.premium*100+"%"}}
        </template>
      </el-table-column>
      <el-table-column label="最小额度" align="center" prop="minimumAmount" />
      <el-table-column label="最大额度" align="center" prop="maxmumAmount" />
      <el-table-column label="是否支持循环借贷" align="center" prop="revolvingLending">
        <template slot-scope="scope">
          <div v-if="scope.row.revolvingLending===1">
            支持
          </div>
          <div v-if="scope.row.revolvingLending===2">
            不支持
          </div>
        </template>
      </el-table-column>
      <el-table-column label="逾期还款日利率" align="center" prop="lateRepayment">
        <template slot-scope="scope">
          {{scope.row.lateRepayment*100+"%"}}
        </template>
      </el-table-column>

      <el-table-column label="允许提前还款(1允许,2不允许)" align="center" prop="earlyRepayment">
        <template slot-scope="scope">
          <div v-if="scope.row.earlyRepayment===1">
            允许
          </div>
          <div v-if="scope.row.earlyRepayment===2">
            不允许
          </div>
        </template>
      </el-table-column>
      <el-table-column label="提前还款违约金比例" align="center" >
        <template slot-scope="scope">

          {{scope.row.earlyRepaymentPercentage*100+"%"}}

          <div v-if="scope.row.earlyRepaymentPercentage===0">

          </div>
        </template>
      </el-table-column>
      <el-table-column label="还款方式" align="center" prop="repaymentMethod">
        <template slot-scope="scope">
          <div v-if="scope.row.repaymentMethod===1">
            等额本金
          </div>
          <div v-if="scope.row.repaymentMethod===2">
            等额本息
          </div>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="shelvesStatus">
        <template slot-scope="scope">
          <div v-if="scope.row.shelvesStatus===1">
            下架中
          </div>
          <div v-if="scope.row.shelvesStatus===2">
            上架中
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改博天金融贷款产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品种类" prop="productVariety">

          <el-input v-model="form.productVariety" placeholder="请输入产品种类-目前只支持信用贷" />
        </el-form-item>
        <el-form-item label="期数" prop="numberLoan">
          <el-input v-model="form.numberLoan" placeholder="请输入期数" />
        </el-form-item>
        <el-form-item label="利率" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入利率" />
        </el-form-item>
        <el-form-item label="手续费" prop="premium">
          <el-input v-model="form.premium" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="最小额度" prop="minimumAmount">
          <el-input v-model="form.minimumAmount" placeholder="请输入最小额度" />
        </el-form-item>
        <el-form-item label="最大额度" prop="maxmumAmount">
          <el-input v-model="form.maxmumAmount" placeholder="请输入最大额度" />
        </el-form-item>
        <el-form-item label="是否支持循环借贷" prop="revolvingLending">
          <el-input v-model="form.revolvingLending" placeholder="请输入是否支持循环借贷(1支持,2不支持)" />
        </el-form-item>
        <el-form-item label="逾期还款" prop="lateRepayment">
          <el-input v-model="form.lateRepayment" placeholder="请输入逾期还款" />
        </el-form-item>
        <el-form-item label="允许提前还款" prop="earlyRepayment">
          <el-input v-model="form.earlyRepayment" placeholder="请输入允许提前还款(1允许,2不允许)" />
        </el-form-item>
        <el-form-item label="提前还款违约金比例" prop="earlyRepaymentPercentage">
          <el-input v-model="form.earlyRepaymentPercentage" placeholder="请输入提前还款违约金比例" />
        </el-form-item>
        <el-form-item label="还款方式" prop="repaymentMethod">
          <el-input v-model="form.repaymentMethod" placeholder="请输入还款方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/product/product";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博天金融贷款产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productVariety: null,
        numberLoan: null,
        rate: null,
        premium: null,
        minimumAmount: null,
        maxmumAmount: null,
        revolvingLending: null,
        loanTimeSetting: null,
        lateRepayment: null,
        earlyRepayment: null,
        earlyRepaymentPercentage: null,
        repaymentMethod: null,
        auditStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询博天金融贷款产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productVariety: null,
        numberLoan: null,
        rate: null,
        premium: null,
        minimumAmount: null,
        maxmumAmount: null,
        revolvingLending: null,
        loanTimeSetting: null,
        lateRepayment: null,
        earlyRepayment: null,
        earlyRepaymentPercentage: null,
        repaymentMethod: null,
        auditStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加博天金融贷款产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改博天金融贷款产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除博天金融贷款产品编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
